package com.jiulan.hearthearing.ui.school.index;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseVPFragment;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.school.adapter.SchoolBookAdapter;
import com.jiulan.hearthearing.ui.school.adapter.SchoolGeanAdapter;
import com.jiulan.hearthearing.ui.school.adapter.SchoolMovieAdapter;
import com.jiulan.hearthearing.ui.school.model.SchoolModel;
import com.jiulan.hearthearing.widget.SpaceItemDecoration;
import com.jiulan.hearthearing.widget.SpaceVerticalItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shehuan.niv.NiceImageView;

import java.util.HashMap;

/**
 * 商学院
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public class SchoolIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private SchoolModel model;
    private SchoolGeanAdapter geanAdapter;
    private SchoolMovieAdapter movieAdapter;
    private SchoolBookAdapter bookAdapter;

    private View view;
    private RelativeLayout rlGean;
    private SmartRefreshLayout smartLayout;
    private NestedScrollView scrollView;
    private LinearLayout llSearch;
    private NiceImageView nivToTop;
    private TextView tvDianying;
    private TextView tvShuji;
    private View vDianying;
    private View vShuji;
    private RecyclerView rcvGean;
    private RecyclerView rcvDianying;
    private RecyclerView rcvShuji;

    public static SchoolIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        SchoolIndex fl = new SchoolIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_school_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    private void initView() {
        rlGean = view.findViewById(R.id.rlGean);
        smartLayout = view.findViewById(R.id.smartLayout);
        scrollView = view.findViewById(R.id.scrollView);
        llSearch = view.findViewById(R.id.llSearch);
        nivToTop = view.findViewById(R.id.nivToTop);
        tvDianying = view.findViewById(R.id.tvDianying);
        tvShuji = view.findViewById(R.id.tvShuji);
        vDianying = view.findViewById(R.id.vDianying);
        vShuji = view.findViewById(R.id.vShuji);
        rcvGean = view.findViewById(R.id.rcvGean);
        rcvDianying = view.findViewById(R.id.rcvDianying);
        rcvShuji = view.findViewById(R.id.rcvShuji);
        rlGean.setOnClickListener(this);
        tvDianying.setOnClickListener(this);
        tvShuji.setOnClickListener(this);
        nivToTop.setOnClickListener(this);
        rcvGean.setLayoutManager(new LinearLayoutManager(context));
        rcvGean.addItemDecoration(new SpaceVerticalItemDecoration(context, R.dimen.fab_margin));
        rcvDianying.setLayoutManager(new GridLayoutManager(context, 2));
        rcvDianying.addItemDecoration(new SpaceItemDecoration(getResources().getDimensionPixelSize(R.dimen.fab_margin), SpaceItemDecoration.GRIDLAYOUT));
        rcvShuji.setLayoutManager(new LinearLayoutManager(context));
        rcvShuji.addItemDecoration(new SpaceVerticalItemDecoration(context, R.dimen.fab_margin));
        changeMode(true);
        smartLayout.setEnableLoadMore(false);
        smartLayout.setEnableNestedScroll(true);
        smartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData(null);
                    }
                }, 1000);
            }
        });
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > oldScrollY) {//下滑
                    nivToTop.setVisibility(View.VISIBLE);
                }
                if (scrollY < oldScrollY) {//上滑

                }
                if (scrollY == 0) {//滑倒顶部
                    nivToTop.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {
        smartLayout.autoRefresh();
    }

    private void getData(String loadingMsg) {
        HashMap hashMap = new HashMap();
        new HttpRequest(context).doGet(UrlConstants.school_index, loadingMsg, hashMap, SchoolModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                smartLayout.finishRefresh();
                if (result instanceof SchoolModel) {
                    model = (SchoolModel) result;
                    setGean();
                    setDianying();
                    setShuji();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private void setShuji() {
        bookAdapter = new SchoolBookAdapter(model.getData().getShuji());
        rcvShuji.setAdapter(bookAdapter);
    }

    private void setDianying() {
        movieAdapter = new SchoolMovieAdapter(model.getData().getDianying());
        rcvDianying.setAdapter(movieAdapter);
    }

    private void setGean() {
        geanAdapter = new SchoolGeanAdapter(model.getData().getGean());
        rcvGean.setAdapter(geanAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlGean:

                break;
            case R.id.tvDianying:
                changeMode(true);
                break;
            case R.id.tvShuji:
                changeMode(false);
                break;
            case R.id.nivToTop:
                scrollView.fling(0);
                scrollView.smoothScrollTo(0, 0);
                break;
        }
    }

    private void changeMode(boolean isSelect) {
        if (isSelect) {
            tvDianying.setTextColor(getResources().getColor(R.color.title_color));
            tvShuji.setTextColor(getResources().getColor(R.color.gray6));
            tvDianying.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.sp_15));
            tvShuji.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.sp_13));
            tvDianying.getPaint().setFakeBoldText(true);
            tvShuji.getPaint().setFakeBoldText(false);
            vDianying.setVisibility(View.VISIBLE);
            vShuji.setVisibility(View.GONE);
            rcvDianying.setVisibility(View.VISIBLE);
            rcvShuji.setVisibility(View.GONE);
        } else {
            tvDianying.setTextColor(getResources().getColor(R.color.gray6));
            tvShuji.setTextColor(getResources().getColor(R.color.title_color));
            tvDianying.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.sp_13));
            tvShuji.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.sp_15));
            tvDianying.getPaint().setFakeBoldText(false);
            tvShuji.getPaint().setFakeBoldText(true);
            vDianying.setVisibility(View.GONE);
            vShuji.setVisibility(View.VISIBLE);
            rcvDianying.setVisibility(View.GONE);
            rcvShuji.setVisibility(View.VISIBLE);
        }
    }
}
